package edu.rutgers.moko.data;

import java.io.Serializable;

import edu.rutgers.moko.data.util.Flag;
import edu.rutgers.moko.data.util.ProtectedAccess;

/**
 * 
 * @author Darja Krushevskaja
 * 
 */

public class ProtectedRule implements Serializable {

	private static final long serialVersionUID = 1460090842546379729L;

	private ProtectedAccess type;

	private Flag flag;

	// location based
	private Integer x1;

	private Integer x2;

	private Integer y1;

	private Integer y2;

	// access point based
	private String apId;

	// client ip based
	private String clientIp;

	private String subnetMask;

	public ProtectedRule(ProtectedAccess type) {
		this.type = type;
	}

	public String getApId() {
		return apId;
	}

	public void setApId(String apId) {
		this.apId = apId;
	}

	public String getClientIp() {
		return clientIp;
	}

	public void setClientIp(String clientIp) {
		this.clientIp = clientIp;
	}

	public String getSubnetMask() {
		return subnetMask;
	}

	public void setSubnetMask(String subnetMask) {
		this.subnetMask = subnetMask;
	}

	public ProtectedAccess getType() {
		return type;
	}

	public void setType(ProtectedAccess type) {
		this.type = type;
	}

	public Integer getX1() {
		return x1;
	}

	public void setX1(Integer x1) {
		this.x1 = x1;
	}

	public Integer getX2() {
		return x2;
	}

	public void setX2(Integer x2) {
		this.x2 = x2;
	}

	public Integer getY1() {
		return y1;
	}

	public void setY1(Integer y1) {
		this.y1 = y1;
	}

	public Integer getY2() {
		return y2;
	}

	public void setY2(Integer y2) {
		this.y2 = y2;
	}

	public Flag getFlag() {
		return flag;
	}

	public void setFlag(Flag flag) {
		this.flag = flag;
	}

	public void validateAndSetControlValues(String loc, String ap, String cli) {
		//add validation
		switch (type) {
		case LOCATION:
			if (loc == null || loc.trim().length() == 0) {
				break;
			}
			String[] coord = loc.split(":");

			if (coord.length != 4) {
				break;
			}
			try {
				this.x1 = Integer.valueOf(coord[0]);
				if(x1<0 || x1>255){
					x1 = null;
				}
				this.y1 = Integer.valueOf(coord[1]);
				if(y1<0 || y1>255){
					y1 = null;
				}
				this.x2 = Integer.valueOf(coord[2]);
				if(x2<0 || x2>255){
					x2 = null;
				}
				this.y2 = Integer.valueOf(coord[3]);
				if(y2<0 || y2>255){
					y2 = null;
				}
			} catch (NumberFormatException e) {
				break;
			}

			break;
		case AP:
			if (ap == null || ap.trim().length() == 0) {
				break;
			} else {
				String [] macs = ap.split(":");
				if(macs.length==6){
					
					this.apId = ap;
				}
				
			}

			break;
		case CLIENT:
			if (cli == null || cli.trim().length() == 0) {
				break;
			}
			String[] cliProp = cli.split(":");

			if (cliProp.length != 2) {
				break;
			} else {
				// TODO:: validate there are valid ips
				this.clientIp = cliProp[0];
				this.subnetMask = cliProp[1];
			}
			break;
		}
	}

	public boolean isValid() {
		Boolean res = false;
		switch (type) {
		case LOCATION:
			if (x1 != null && y1 != null && x2 != null && y2 != null){
				res = true;
			}
			break;
		case AP:
			if (apId!=null){
				res = true;
			}
			break;
		case CLIENT:
			if (clientIp != null && subnetMask != null){
				res = true;
			}
			break;
		
		}
		
		return res;
	}
	
	public String getAsRule(){
		StringBuilder res = new StringBuilder();
		
		res.append(type.field()).append("\t");
		res.append(flag.title()).append("\t");
		switch (type) {
		case LOCATION:
			res.append(x1).append(":");
			res.append(y1).append(":");
			res.append(x2).append(":");
			res.append(y2);
			break;
		case AP:
			res.append(apId);
			break;

		case CLIENT:
			res.append(clientIp).append(":");
			res.append(subnetMask);
			break;
		}
		res.append(";");
		return res.toString();
	}
}
